<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <script>
    //安全的工厂方法类
    var Factory = function (type, content) {
      if (this instanceof Factory) {
        var o = new this[type](content);
        return o;
      } else {
        return new Factory(type, content)
      }
    }
    Factory.prototype = {
      JavaScript: function (content) {
        this.content = content
        this.show = function () {
          console.log(this.content);
        }
      },
      Java: function (content) {
        this.content = content;
        this.show = function () {
          console.log(this.content);
        }
      }
    }

    var o = Factory('JavaScript', 'Hello FrontEnd');
    o.show();
  </script>

</body>

</html>